package com.huike.clues.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huike.clues.domain.SysNotice;
import com.huike.clues.domain.SysNoticePageVO;
import com.huike.clues.domain.result.SysNoticePageResult;
import com.huike.clues.mapper.NoticeMapper;
import com.huike.clues.service.NoticeService;
import com.huike.common.constant.ScheduleConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description noticeServiceImpl
 * @Author bin
 * @Date 2023-10-15
 */
@Service
@RequiredArgsConstructor
public class NoticeServiceImpl implements NoticeService {

    private final NoticeMapper noticeMapper;


    /**
     * 处理获取系统提醒分页列表
     * @param status
     * @return
     */
    @Override
    public SysNoticePageResult pagelist(Integer status) {
        //1. 设置当前页与页面大小
        Page<SysNotice> page = new Page<>(0,20);
        //2. 创建分页需要条件
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("status",status);
        queryWrapper.orderByDesc("create_time");

        //3. 调用selectPage方法实现分页，分页的结果会被封装到Page对象中
        noticeMapper.selectPage(page,queryWrapper);

        List<SysNotice> sysNoticeList = new ArrayList<>(page.getRecords());

        SysNoticePageVO sysNoticePageVO = SysNoticePageVO.builder()
                .rows(sysNoticeList)
                .total(page.getTotal())
                .build();


        SysNoticePageResult sysNoticePageResult = new SysNoticePageResult();
        BeanUtils.copyProperties(sysNoticePageVO,sysNoticePageResult);

        return sysNoticePageResult;
    }


    /**
     * 处理获取系统提醒下拉列表
     * @param status
     * @return
     */
    @Override
    public List<SysNotice> list(Integer status) {
        //1. 设置当前页与页面大小
        Page<SysNotice> page = new Page<>(0,20);
        //2. 创建分页需要条件
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("status",status);

        //3. 调用selectPage方法实现分页，分页的结果会被封装到Page对象中
        noticeMapper.selectPage(page,queryWrapper);

        List<SysNotice> sysNoticeList = new ArrayList<>(page.getRecords());

        return sysNoticeList;
    }


    /**
     * 获取详情信息
     * @param noticeId
     * @return
     */
    @Override
    public SysNotice noticeId(Integer noticeId) {

        // 根据noticeId查询详细信息
        return noticeMapper.selectById(noticeId);
    }


    /**
     * 未读变已读
     * @param noticeId
     */
    @Override
    public void updateNoticeId(Integer noticeId) {
        SysNotice sysNotice = new SysNotice();
        sysNotice.setNoticeId(noticeId);
        //  ScheduleConstants.MISFIRE_IGNORE_MISFIRES 这个是字符串的”1“
        //  我找不到其他的”1“了又不知道写去那就将就用了 实际意思是将状态改为已读
        sysNotice.setStatus(ScheduleConstants.MISFIRE_IGNORE_MISFIRES);
        noticeMapper.updateById(sysNotice);

    }



}
